package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Hospital;
import exception.YyghException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Ricard Tang
 * @Date: 2022/10/24/15:49
 * @Description:
 */
@RestController
@RequestMapping("api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;

    //查询医院接口
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){

        Map<String,String[]> requestMap = request.getParameterMap();
        //将requestMap中的String[]转换为Object
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //验证签名是否一致
        String hoscode = (String) paramMap.get("hoscode");
        //获取医院签名 -- 经过MD5加密
        String hospSign = (String) paramMap.get("sign");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!hospSign.equals(signKeyMD5))
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);

    }
    

    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        Map<String,String[]> requestMap = request.getParameterMap();
        //将requestMap中的String[]转换为Object
        Map<String,Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院签名 -- 经过MD5加密
        //String hospSign = (String) paramMap.get("sign");
        //根据医院编码 hoscode 查询数据库中的签名
        //String hoscode = (String) paramMap.get("hoscode");
        hospitalService.saveHosp(paramMap);
        return Result.ok();
    }

}
